﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ini;

namespace TwipPlays
{
    public partial class Keys : Form
    {
        public Keys()
        {

            InitializeComponent();

            // Initialize values
            IniFile ini = new IniFile("irc_parser/config.ini");

            bool multiAllowed = (ini.IniReadValue("keys", "multi-allowed") == "true");
            bool multiVotingOnly = (ini.IniReadValue("keys", "multiVotingOnly") == "true");

            int maxActions, maxRepeats;
            string maxActionsStr = ini.IniReadValue("keys", "maxActions");
            string maxRepeatsStr = ini.IniReadValue("keys", "maxRepeats");

            int.TryParse(maxActionsStr, out maxActions);
            int.TryParse(maxRepeatsStr, out maxRepeats);


            string allowed = ini.IniReadValue("keys", "allowed");

            string[] separators = { ";" };
            string[] splitKeys = allowed.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            foreach (string key in splitKeys)
                this.key_grid.Rows.Add(new string[] { key });

            this.max_actions_spin.Value = maxActions;
            this.max_repeats_spin.Value = maxRepeats;
            this.compound_actions_check.Checked = multiAllowed;
            this.compound_voting_check.Checked = multiVotingOnly;

        }

        private void save_clicked(object sender, EventArgs e)
        {

            // Initialize values
            IniFile ini = new IniFile("irc_parser/config.ini");

            string multiAllowed = (this.compound_actions_check.Checked ? "true" : "false");
            string multiVotingOnly = (this.compound_voting_check.Checked ? "true" : "false");
            string maxActions = this.max_actions_spin.Value.ToString();
            string maxRepeats = this.max_repeats_spin.Value.ToString();


            List<string> allowedArr = new List<string>();
            foreach (DataGridViewRow row in this.key_grid.Rows)
                allowedArr.Add(row.Cells[0].Value.ToString());

            string sep = ";";
            string allowed = String.Join(sep, allowedArr);


            ini.IniWriteValue("keys", "multi-allowed", multiAllowed);
            ini.IniWriteValue("keys", "multiVotingOnly", multiVotingOnly);
            ini.IniWriteValue("keys", "maxActions", maxActions);
            ini.IniWriteValue("keys", "allowed", allowed);
            ini.IniWriteValue("keys", "maxRepeats", maxRepeats);

            this.Close();

        }

        private void cancel_clicked(object sender, EventArgs e)
        {

            this.Close();

        }

        private void add_clicked(object sender, EventArgs e)
        {

            string key = this.key_entry.Text.ToLower();

            // Search for the key
            bool found = false;
            foreach (DataGridViewRow row in this.key_grid.Rows )
            {

                if (row.Cells[0].Value.ToString().ToLower().Equals(key))
                {

                    found = true;
                    break;

                }

            }

            if( !found )
                this.key_grid.Rows.Add(new string[] { key });

            this.key_entry.Text = "";

        }

        private void remove_clicked(object sender, EventArgs e)
        {

            this.key_grid.Rows.Remove(this.key_grid.SelectedRows[0]);

        }

        private void key_grid_selection_changed(object sender, EventArgs e)
        {

            this.key_entry.Text = this.key_grid.SelectedRows[0].Cells[0].Value.ToString();

        }
    }
}
